﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableParsers;

namespace Speakup
{
    [Serializable]
    public class UnitResultsEntry
    {
        public string Course;
        public int Unit;
        public int? Grammar;
        public int? Vocabulary;
        public int? Listening;
        public int? Reading;
        public int Avarage;
    }

    [Serializable]
    public class UnitResultsTable : List<UnitResultsEntry>
    {
        public static UnitResultsTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebPageParser.WebPageCache pageCache = new WebPageParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }

        public static UnitResultsTable Fetch(WebPageParser.WebPageCache pageCache)
        {
            ResultsTable result = ResultsTable.Fetch(pageCache);
            return Create(result);
        }

        public static UnitResultsTable Create(ResultsTable table)
        {
            UnitResultsTable unitResultTable = new UnitResultsTable();

            foreach (var entry in table)
            {
                if (entry.IsUnit)
                {
                    UnitResultsEntry unitEntry = new UnitResultsEntry();
                    unitEntry.Course = entry.Course;
                    unitEntry.Unit = entry.UnitOrLevel;
                    unitEntry.Grammar = entry.Grammar;
                    unitEntry.Vocabulary = entry.Vocabulary;
                    unitEntry.Listening = entry.Listening;
                    unitEntry.Reading = entry.Reading;
                    unitEntry.Avarage = entry.Avarage;

                    unitResultTable.Add(unitEntry);
                }
            }

            return unitResultTable;
        }
    }
}
